<?php 
/**
 * This file is part of DomAr.
 * 
 * DomAr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomAr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomAr.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* @package DomAr
* @subpackage Validators
*/
class DomArValidator_Password extends DomArValidator {

	/**
	* Check criterias: has lower case letters, has upper case letters, has numbers and length
	*
	* Strength values:
	* <ul>
	* <li>3 means that password must have all criterias except length. Allowed are: AAAaaa000 </li>
	* <li>2 means that password must have at least 2 criterias except length. Allowed are: AAAaaa, AAA000, aaa000 </li>
	* <li>1 means that password must have at least 1 criteria except length. Allowed are: AAA,aaa,000 </li>
	* <li>0 means that no criteria checks are done. (Only length is checked) </li>
	* </ul>
	*
	* @param String $value
	* @param Int $strength [1,2,3]
	* @param Int $length Password minimum length
	* @return Int 0 on ok, 115 if too short, 116 if doesn't containt enaugh symbols
	*/
	public static function validate( $value, $strength = 2, $length = 6 ) {
		
		if( strlen($value) < $length ) 
			return 115;
		
		if( $strength <= 0 )
			return 0;
			
		for( $i=0; $i < strlen($value); $i++ ) {
			$hasLowerLetters = (int)(bool)preg_match('/[a-z]/', $value);
			$hasUpperLetters = (int)(bool)preg_match('/[A-Z]/', $value);
			$hasNumbers = (int)(bool)preg_match('/[0-9]/', $value);
		}
		
		return ($hasLowerLetters + $hasUpperLetters + $hasNumbers >= $strength) ? 0 : 116;
	}


}
?>